package com.example.aixiangshou.runninggame;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.x_memteor.base_master.application.MyApplication;
import com.x_memteor.base_master.bean.ItemBean;
import com.x_memteor.base_master.bean.ScorBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NombreAdapter extends BaseQuickAdapter<NombreBean, BaseViewHolder> {
    private AnimatorSet mLeftInSet;
    private AnimatorSet mRightOutSet;
    private boolean mIsShowBack;
    private boolean isClick;
    List<NombreBean> beanList;
    private int gemTimes;
    List<ScorBean> scorBeanList;
    private List<ItemBean> list;

    private String[] strs;
    private Integer[] icons;
    private int size;

    public NombreAdapter(int layoutResId, @Nullable List<NombreBean> data) {
        super(layoutResId, data);
        beanList = data;
        gemTimes = MyApplication.getInstance().getGameBean().getTurnuration();

        scorBeanList = new ArrayList<>();

        if (MyApplication.getInstance().getGameBean() != null && MyApplication.getInstance().getGameBean().getList() != null) {
            list = MyApplication.getInstance().getGameBean().getList();
            size = MyApplication.getInstance().getGameBean().getList().size();
            strs = new String[size];
            icons = new Integer[size];
            gemTimes = MyApplication.getInstance().getGameBean().getGameDuration();
        }

        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                ItemBean itemBean = list.get(i);
                strs[i] = list.get(i).getName();
                icons[i] = list.get(i).getIcon();
                scorBeanList.add(new ScorBean(itemBean.getName(), itemBean.getIcon(), 0));
            }
        }
    }

    @Override
    protected void convert(final BaseViewHolder helper, NombreBean item) {
        helper.setImageDrawable(R.id.iv_front, item.mFront);
        helper.setImageDrawable(R.id.iv_back, item.mBack);

        ImageView ivFront = helper.getView(R.id.iv_front);

        final RelativeLayout flFront = helper.getView(R.id.main_fl_card_front);
        final RelativeLayout flBack = helper.getView(R.id.main_fl_card_back);
        final FrameLayout flMain = helper.getView(R.id.main_fl_container);

//        flMain.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                setFlipCard(flFront, flBack);
//            }
//        });
        ivFront.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAnimators(mContext, flMain);
                setCameraDistance(mContext, flFront, flBack);
                setFlipCard(flFront, flBack);
                if (isClick) {
                    if (gemTimes > 0) {
                        gemTimes--;
                        int i = 0;
                        for (int i1 = 0; i1 < beanList.size(); i1++) {
                            if (beanList.get(helper.getPosition()).mName == scorBeanList.get(i1).getIcon()) {
                                i = scorBeanList.get(i1).getTime();
                                scorBeanList.get(i1).setTime(i + 1);
                            }
                        }
                        Collections.shuffle(beanList);
                        NombreAdapter.this.notifyDataSetChanged();
                    }
                    if (gemTimes == 0) {
                        MyApplication.getInstance().getGameBean().setScorBeanList(scorBeanList);
                        mContext.startActivity(new Intent(mContext, ShowScoreActivity.class));
                    }
                }
            }
        });
    }

    // 设置动画
    public void setAnimators(Context context, final FrameLayout mFlContainer) {
        mLeftInSet = (AnimatorSet) AnimatorInflater.loadAnimator(context, R.animator.anim_in);
        mRightOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(context, R.animator.anim_out);

        // 设置点击事件
        mRightOutSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mFlContainer.setClickable(false);
            }
        });
        mLeftInSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mFlContainer.setClickable(true);
            }
        });
    }

    // 改变视角距离, 贴近屏幕
    public static void setCameraDistance(Context context, RelativeLayout mFlCardFront_, RelativeLayout mFlCardBack_) {
        int distance = 16000;
        float scale = context.getResources().getDisplayMetrics().density * distance;
        mFlCardFront_.setCameraDistance(scale);
        mFlCardBack_.setCameraDistance(scale);

    }

    public void setFlipCard(RelativeLayout mFlCardFront, RelativeLayout mFlCardBack) {
        // 正面朝上
        if (!mIsShowBack) {
            mRightOutSet.setTarget(mFlCardFront);
            mLeftInSet.setTarget(mFlCardBack);
            mRightOutSet.start();
            mLeftInSet.start();
            mIsShowBack = true;
            isClick = false;
        } else { // 背面朝上
            mRightOutSet.setTarget(mFlCardBack);
            mLeftInSet.setTarget(mFlCardFront);
            mRightOutSet.start();
            mLeftInSet.start();
            mIsShowBack = false;
            isClick = true;
        }
    }

    @Override
    protected void startAnim(Animator anim, int index) {
        super.startAnim(anim, index);
        if (index < 10)
            anim.setStartDelay(index * 500);
    }
}
